<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../org/angular-ui-router.min.js"></script>
    <script src="../org/angular.min.js"></script>
    <script src="../org/jquery.min.js"></script>
</head>

<body>


    <script>

        angular.module('vm',['ui.router'])
        .config(['stateProvider','urlRouterProvider',function($stateProvider,$urlRouterProvider){
            //设置默认监听
            $urlRouterProvider.otherwise('home');
            //设置路由规则

            $stateProvider.state('Lwj',{
                url: '/home',
                template: 'view/home.html',
                controller: 'ctrl'
            })
            .state('user',{
                url: '/user/{id}',
                template: '<h1>用户名：{{user.name}}</h1>',
                controller: 'ctrl'
            })
        }])
        .controller('ctrl',['$scope','$stateParams',function($scope,$stateParams){
            $scope.user = [
                {id: 1, name: '123'},
                {id: 2, name: '456'}
            ]
            //获取url锚点参数id
            id = $stateParams.id;
            //根据id重新赋值
            if(id){
                for(var i=0;i<$scope.user.length;i++){
                   if(id==$scope.user[i].id){
                       $scope.user = $scope.user[i];
                   }
                }
            }
        }])
    </script>

</body>

</html>